<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="店铺">
              <j-search-select-tag placeholder="请选择店铺" v-model="queryParam.shopId" dict="lg_shop_roster,ec_name,id" :async="true"></j-search-select-tag>
               <!--<j-dict-select-tag type="list" placeholder="请选择店铺" v-model="queryParam.shopId" dictCode="lg_shop_roster,ec_name,id" />-->
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="账期">
              <a-month-picker
                valueFormat="YYYY-MM"
                placeholder="请选择账期"
                v-model="queryParam.billingCycle"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="结算组织">
                <j-search-select-tag placeholder="请选择结算组织" v-model="queryParam.orgId" dict="lg_base_settlement_org,name,id" :async="true"></j-search-select-tag>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="数据来源">
                <j-dict-select-tag placeholder="请选择数据来源" v-model="queryParam.dataSource" dictCode="finance_amazon_order_summary_data_source"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="币种">
                <j-search-select-tag placeholder="请选择币种" v-model="queryParam.currencyType" dictCode="lg_k3cloud_currency,fname,fcode"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="币种">
                <j-dict-select-tag placeholder="请选择类型" v-model="queryParam.type" dictCode="finance_amazon_order_summary_type"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
     <!-- <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button> -->
      <a-button @click="handleShowCreateMonth" type="primary" icon="audit">生成接口数据</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('亚马逊Payment汇总')">导出</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('亚马逊Payment汇总', 'template')">导出模板</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange"
        :customRow="selectRowData">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;" :preview="0"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <!-- <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown> -->               
           <a @click="handleDetail(record)">详情</a>
        </span>

      </a-table>
    </div>

    <finance-amazon-order-summary-modal ref="modalForm" @ok="modalFormOk"></finance-amazon-order-summary-modal>

    <a-modal
      title="请选择生成数据的月份"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleHideCreateMonth"
    >
      <j-form-container>
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="24">
              <a-form-model-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billingCycle">
                <a-month-picker
                  valueFormat="YYYY-MM"
                  placeholder="请选择月份"
                  style="width: 100%"
                  v-model="model.billingCycle"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
    </a-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import FinanceAmazonOrderSummaryModal from './modules/FinanceAmazonOrderSummaryModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import {postAction} from "@api/manage";

  export default {
    name: 'FinanceAmazonOrderSummaryList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      FinanceAmazonOrderSummaryModal
    },
    data () {
      return {
        description: '亚马逊Payment汇总管理页面',
        model: {},
        visible: false,
        confirmLoading: false,
        validatorRules: {
          billingCycle: [{ required: true, message: '请选择月份!' }],
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        labelCols: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'店铺',
            align:"center",
            sorter: true,
            dataIndex: 'shopId_dictText'
          },
          {
            title:'账期',
            align:"center",
            sorter: true,
            dataIndex: 'billingCycle',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'结算组织',
            align:"center",
            sorter: true,
            dataIndex: 'orgId_dictText'
          },
          {
            title:'币种',
            align:"center",
            sorter: true,
            dataIndex: 'currencyType_dictText'
          },
          {
            title:'数据来源',
            align:"center",
            sorter: true,
            dataIndex: 'dataSource_dictText'
          },
          {
            title:'类型',
            align:"center",
            sorter: true,
            dataIndex: 'type_dictText'
          },
          {
            title:'产品金额',
            align:"center",
            dataIndex: 'productAmount'
          },
          {
            title:'产品金额税费',
            align:"center",
            dataIndex: 'productAmountTax'
          },
          {
            title:'客户运费',
            align:"center",
            dataIndex: 'customerFreight'
          },
          {
            title:'客户运费税费',
            align:"center",
            dataIndex: 'customerFreightTax'
          },
          {
            title:'买家支付的礼品包装费',
            align:"center",
            dataIndex: 'giftFee'
          },
          {
            title:'买家支付的礼品包装费税费',
            align:"center",
            dataIndex: 'giftFeeTax'
          },
          {
            title:'亚马逊积分费用',
            align:"center",
            dataIndex: 'amazonIntegralFee'
          },
          {
            title:'折扣促销',
            align:"center",
            dataIndex: 'discount'
          },
          {
            title:'折扣促销税费',
            align:"center",
            dataIndex: 'discountTax'
          },
          {
            title:'亚马逊税费',
            align:"center",
            dataIndex: 'amazonTax'
          },
          {
            title:'平台手续费',
            align:"center",
            dataIndex: 'platformFee'
          },
          {
            title:'FBA手续费',
            align:"center",
            dataIndex: 'fbaFee'
          },
          {
            title:'月租',
            align:"center",
            dataIndex: 'monthlyFee'
          },
          {
            title:'广告费',
            align:"center",
            dataIndex: 'advertisingFee'
          },
          {
            title:'提现',
            align:"center",
            dataIndex: 'withdraw'
          },
          {
            title:'其他交易费',
            align:"center",
            dataIndex: 'otherFee'
          },
          {
            title:'其他',
            align:"center",
            dataIndex: 'other'
          },
          {
            title:'创建人',
            align:"center",
            dataIndex: 'createBy'
          },
          {
            title:'创建日期',
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title:'更新人',
            align:"center",
            dataIndex: 'updateBy'
          },
          {
            title:'更新日期',
            align:"center",
            dataIndex: 'updateTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:110,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/finance/financeAmazonOrderSummary/list",
          delete: "/finance/financeAmazonOrderSummary/delete",
          deleteBatch: "/finance/financeAmazonOrderSummary/deleteBatch",
          createUrl: "/finance/financeAmazonOrderSummary/create",
          exportXlsUrl: "/finance/financeAmazonOrderSummary/exportXls",
          exportTemplateXlsUrl: "/finance/financeAmazonOrderSummary/exportTemplateXls",
          importExcelUrl: "finance/financeAmazonOrderSummary/importExcel",
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      selectRowData(record,index){
        const that = this
        return {
          on:{
            dblclick:() => {
              this.handleDetail(record)
            }
          }
        }
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'shopId',text:'店铺',dictCode:'lg_shop_roster,ec_name,id'})
        fieldList.push({type:'date',value:'billingCycle',text:'账期'})
        fieldList.push({type:'string',value:'orgId',text:'结算组织',dictCode:'lg_base_settlement_org,name,id'})
        fieldList.push({type:'string',value:'dataSource',text:'数据来源',dictCode:''})
        fieldList.push({type:'string',value:'currencyType',text:'币种',dictCode:'lg_currency,display_name,currency_code'})
        fieldList.push({type:'BigDecimal',value:'productAmount',text:'费用类型',dictCode:'sys_category,name,code'})
        fieldList.push({type:'BigDecimal',value:'productAmountTax',text:'产品金额税费',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'customerFreight',text:'客户运费',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'customerFreightTax',text:'客户运费税费',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'giftFee',text:'买家支付的礼品包装费',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'giftFeeTax',text:'买家支付的礼品包装费税费',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'amazonIntegralFee',text:'亚马逊积分费用',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'discount',text:'折扣促销',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'discountTax',text:'折扣促销税费',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'amazonTax',text:'亚马逊税费',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'platformFee',text:'平台手续费',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'fbaFee',text:'FBA手续费',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'monthlyFee',text:'月租',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'advertisingFee',text:'广告费',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'withdraw',text:'提现',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'otherFee',text:'其他交易费',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'other',text:'其他',dictCode:''})
        fieldList.push({type:'string',value:'createBy',text:'创建人',dictCode:''})
        fieldList.push({type:'datetime',value:'createTime',text:'创建日期'})
        fieldList.push({type:'string',value:'updateBy',text:'更新人',dictCode:''})
        fieldList.push({type:'datetime',value:'updateTime',text:'更新日期'})
        this.superFieldList = fieldList
      },
      handleShowCreateMonth() {
        this.visible = true
      },
      handleHideCreateMonth() {
        this.visible = false
      },
      handleOk() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.confirmLoading = true
            postAction(this.url.createUrl, {
              billingCycle: this.model.billingCycle,
            })
              .then((res) => {
                if (res.success) {
                  this.loadData()
                  this.$message.success(res.message)
                } else {
                  this.$message.warning(res.message)
                }
              })
              .finally(() => {
                this.loading = false
                this.confirmLoading = false
                this.visible = false
              })
          }
        })
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>